package com.example.study.rabbitmq.config;

import com.example.study.config.redis.RedisUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName ReceiveHandler
 * @Description TODO
 * @Author 胡泽
 * @Date 2019/12/17 13:02
 * @Version 1.0
 */
@Slf4j
//@Component
public class ReceiveHandler {

//    @Autowired
//    RedisUtil redisUtil;

//    @RabbitListener(queues = {RabbitmqInfo.queue1})
    public String receive_email_(Object msg, Message message, Channel channel) {
        log.info("应答:email " + message.getMessageProperties().getConsumerQueue());
        return message.getMessageProperties().getConsumerQueue();
    }

    //监听sms队列
//    @RabbitListener(queues = {RabbitmqInfo.queue2})
    public void receive_sms(Object msg, Message message, Channel channel) {
        log.info("应答:sms " + message.getMessageProperties().getConsumerQueue());
    }

//    @RabbitListener(queues = {RabbitmqInfo.queue3})
    public void receive_weixin(Object msg, Message message, Channel channel) {
        log.info("应答:weixin " + message.getMessageProperties().getConsumerQueue());
    }

    //    @RabbitListener(bindings = @QueueBinding(
//        // email.fanout.queue 是队列名字，这个名字你可以自定随便定义。
//        value = @Queue(value = "queue_发布订阅模式_1", autoDelete = "false"),
//        // order.fanout 交换机的名字 必须和生产者保持一致
//        exchange = @Exchange(value = "exchange_fanout_发布订阅模式",
//                // 这里是确定的rabbitmq模式是：fanout 是以广播模式 、 发布订阅模式
//                type = ExchangeTypes.FANOUT)


//        监听email队列
//    @RabbitListener(queues = {RabbitmqInfo.queue1})
//    public void receive_email(Object msg, Message message, Channel channel) {
////        log.info("QUEUE_INFORM_EMAIL msg" + msg + "\n" + message + "\n" + message.getBody());
//        /*
//         * 确认应答
//         * basicAck(long deliveryTag, boolean multiple)
//         *   deliveryTag：消息编号，用于确认是哪条消息
//         *   multiple：应答多个消息还是应答一个消息
//         *       true：把当前消息和之前的消息一起应答
//         *       false：只应答当前消息
//         *
//         * */
//            try {
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//                log.info("应答:email "+message.getMessageProperties().getConsumerQueue());
//            } catch (IOException e) {
//                log.error("应答失败");
//                throw new RuntimeException(e);
//            }
//    }
}